﻿using System;
using System.Data;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using Ch.Admin.Bfs.Sms.Sdmx.Utils;

namespace Ch.Admin.Bfs.Sms.Sdmx.Extension.CodeBook.Control
{
    public partial class UcInternationalString : UserControl
    {
        public IWindowsFormsEditorService _wfes;

        public InternationalizedString MultilangString { get; set; }

        private DataTable tab = null;
        private string[] defaultLangSet;

        public UcInternationalString()
        {
            MultilangString = new InternationalizedString();
            Init();
        }

        public UcInternationalString(InternationalizedString interString)
        {
            MultilangString = interString == null ? new InternationalizedString() : interString;
            Init();
        }

        private void Init()
        {
            InitializeComponent();
            tab = new DataTable();
            tab.Columns.Add(new DataColumn("Lang"));
            tab.Columns.Add(new DataColumn("Column"));

            dataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            defaultLangSet = new string[] { "en", "de", "fr", "it" };
            foreach (string lg in defaultLangSet)
                tab.Rows.Add(new object[] { lg, MultilangString[lg] });
            this.dataGridView1.DataSource = tab;

            this.dataGridView1.Rows[0].Cells[1].Selected = true;
        }

        private void UcInternationalString_Leave(object sender, EventArgs e)
        {
            dataGridView1.EndEdit();

            if (MultilangString != null)
            {
                MultilangString.Clear();
                foreach (DataRow r in tab.Rows)
                {
                    if (r[1].ToString().Trim().Length > 0)
                        MultilangString.Add(r[0].ToString().Trim(), r[1].ToString().Trim());
                }
            }
        }

    }
}
